//
//  LZMyRestaurantInfoTimeTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyRestaurantInfoTimeTableViewCell.h"
#import "Masonry.h"
#import "LZMerchantInfoModel.h"
#import "LZCouponListModel.h"
#import "LZDateFormatterManager.h"
#import "NSDate+TimeCategory.h"

@interface LZMyRestaurantInfoTimeTableViewCell()

@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UILabel * startLabel;
@property (nonatomic, strong) UILabel * subLabel;
@property (nonatomic, strong) UILabel * endLabel;
@property (nonatomic, strong) UIView * line;
@property (nonatomic, strong) UIImageView * accessoryImageView;

@end

@implementation LZMyRestaurantInfoTimeTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.startLabel];
    [self.contentView addSubview:self.subLabel];
    [self.contentView addSubview:self.endLabel];
    [self.contentView addSubview:self.line];
    [self.contentView addSubview:self.accessoryImageView];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(10);
    }];
    
    [self.startLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(weakSelf.titleLabel.mas_trailing).with.offset(10);
        make.trailing.mas_equalTo(weakSelf.subLabel.mas_leading);
        make.height.mas_equalTo(44);
    }];
    
    [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(weakSelf.startLabel.mas_trailing);
        make.width.mas_lessThanOrEqualTo(20);
    }];
    
    [self.endLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(weakSelf.subLabel.mas_trailing);
        make.trailing.mas_lessThanOrEqualTo(weakSelf.accessoryImageView.mas_leading).with.offset(-10);
        make.height.mas_equalTo(44);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
    
    [self.accessoryImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
        make.trailing.mas_equalTo(-10);
        make.size.mas_equalTo(CGSizeMake(6, 12));
    }];
}

- (void)tapGR:(UITapGestureRecognizer *)sender {
    UIView * v = sender.view;
    if (_tapGRActionBlock) {
        self.tapGRActionBlock(v);
    }
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor   = UIColorFromRGB(0x333333, 1);
        _titleLabel.font        = [UIFont systemFontOfSize:15];
    }
    
    return _titleLabel;
}

-(UILabel *)startLabel {
    
    if (_startLabel == nil) {
        _startLabel = [[UILabel alloc] init];
        _startLabel.textColor        = UIColorFromRGB(0xcccccc, 1);
        _startLabel.font             = [UIFont systemFontOfSize:15];
        _startLabel.userInteractionEnabled = YES;
        _startLabel.tag = 1000;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [_startLabel addGestureRecognizer:tap];
    }
    
    return _startLabel;
}

- (UILabel *)subLabel {
    
    if (_subLabel == nil) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.textColor         = UIColorFromRGB(0x333333, 1);
        _subLabel.font              = [UIFont systemFontOfSize:15];
        _subLabel.textAlignment     = NSTextAlignmentCenter;
        _subLabel.text = @"~";
    }
    
    return _subLabel;
}

- (UILabel *)endLabel {
    
    if (_endLabel == nil) {
        _endLabel = [[UILabel alloc] init];
        _endLabel.textColor        = UIColorFromRGB(0xcccccc, 1);
        _endLabel.font             = [UIFont systemFontOfSize:15];
        _endLabel.userInteractionEnabled = YES;
        _endLabel.tag = 2000;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
        [_endLabel addGestureRecognizer:tap];
    }
    
    return _endLabel;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (UIImageView *)accessoryImageView {
    
    if (_accessoryImageView == nil) {
        _accessoryImageView = [[UIImageView alloc] init];
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_ar"];
        } else {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_en"];
        }
    }
    
    return _accessoryImageView;
}

- (void)setMerchantInfoModel:(LZMerchantInfoModel *)merchantInfoModel {

    self.titleLabel.text = LZLocalizedString(@"open_time", nil);
    
    if (merchantInfoModel) {
        _merchantInfoModel = merchantInfoModel;
        
        DLog(@"%@, %@", merchantInfoModel.openTime, merchantInfoModel.closeTime);
        
        if (merchantInfoModel.openTime && merchantInfoModel.openTime.length > 0) {
            self.startLabel.text = [[LZDateFormatterManager shareInstance] stringFromStringNormalHaveLineHHmm:merchantInfoModel.openTime];
            self.startLabel.textColor = UIColorFromRGB(0x333333, 1);
        } else {
            self.startLabel.text = LZLocalizedString(@"start_date", nil);
            self.startLabel.textColor = UIColorFromRGB(0xcccccc, 1);
        }
        
        if (merchantInfoModel.closeTime && merchantInfoModel.closeTime.length > 0) {
            self.endLabel.text = [[LZDateFormatterManager shareInstance] stringFromStringNormalHaveLineHHmm:merchantInfoModel.closeTime];
            self.endLabel.textColor = UIColorFromRGB(0x333333, 1);
        } else {
            self.endLabel.text = LZLocalizedString(@"finish_date", nil);
            self.endLabel.textColor = UIColorFromRGB(0xcccccc, 1);
        }
    }
}

- (void)setCouponListModel:(LZCouponListModel *)couponListModel {

    if (couponListModel) {
        _couponListModel = couponListModel;
    
        if (self.tag == 4) {
            self.titleLabel.text = LZLocalizedString(@"valid_from_to", @"");
            if (couponListModel.couponsStartTime) {
                self.startLabel.text = [NSDate dateStrFromCstampTime:couponListModel.couponsStartTime/1000 withDateFormat:@"dd-MM-yyyy"];
                self.startLabel.textColor = UIColorFromRGB(0x333333, 1);
            } else {
                self.startLabel.text = LZLocalizedString(@"start_date", nil);
                self.startLabel.textColor = UIColorFromRGB(0xcccccc, 1);
            }
            
            if (couponListModel.couponsEndTime) {
                self.endLabel.text = [NSDate dateStrFromCstampTime:couponListModel.couponsEndTime/1000 withDateFormat:@"dd-MM-yyyy"];
                self.endLabel.textColor = UIColorFromRGB(0x333333, 1);
            } else {
                self.endLabel.text = LZLocalizedString(@"finish_date", nil);
                self.endLabel.textColor = UIColorFromRGB(0xcccccc, 1);
            }
        } else {
            
            self.titleLabel.text = LZLocalizedString(@"everyday_valid", @"");
            
            if (self.tag == 6) {
                if (couponListModel.couponsStartDaytime) {
                    self.startLabel.text = couponListModel.couponsStartDaytime;
                    self.startLabel.textColor = UIColorFromRGB(0x333333, 1);
                } else {
                    self.startLabel.text = LZLocalizedString(@"from_time", nil);
                    self.startLabel.textColor = UIColorFromRGB(0xcccccc, 1);
                }
                
                if (couponListModel.couponsEndDaytime) {
                    self.endLabel.text = couponListModel.couponsEndDaytime;
                    self.endLabel.textColor = UIColorFromRGB(0x333333, 1);
                } else {
                    self.endLabel.text = LZLocalizedString(@"to_time", nil);
                    self.endLabel.textColor = UIColorFromRGB(0xcccccc, 1);
                }
            } else {//group everyday time
                if (couponListModel.grouponStartDaytime) {
                    self.startLabel.text = couponListModel.grouponStartDaytime;
                    self.startLabel.textColor = UIColorFromRGB(0x333333, 1);
                } else {
                    self.startLabel.text = LZLocalizedString(@"from_time", nil);;
                    self.startLabel.textColor = UIColorFromRGB(0xcccccc, 1);
                }
                
                if (couponListModel.grouponEndDaytime) {
                    self.endLabel.text = couponListModel.grouponEndDaytime;
                    self.endLabel.textColor = UIColorFromRGB(0x333333, 1);
                } else {
                    self.endLabel.text = LZLocalizedString(@"to_time", nil);;
                    self.endLabel.textColor = UIColorFromRGB(0xcccccc, 1);
                }
            }
        }
    }
}

@end
